package com.stack.knowyouservice.service.impl;


import com.stack.knowyoubase.anotional.TimeOut;
import com.stack.knowyoubean.bean.User;
import com.stack.knowyoudao.dao.UserDao;
import com.stack.knowyouservice.service.UserService;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;


/**
 * 教师表的服务层
 * (业务层统一向controller抛出异常，此时捕获异常会造成事务失败)
 * 事务传播行为用来描述由某一个事务传播行为修饰的方法被嵌套进另一个方法的时事务如何传播(存在事务嵌套的地方)
 * 事务控制可以在方法也可指定在类上（要求指定在方法上）
 * 实现多接口采用cglib 基于子类的动态代理
 * userService层的账号userId通过认证角色获取，在认证时对类型装换异常进行过处理，不存在类型装换问题
 *
 * @author stack
 */
@Service("userService")
@Scope("singleton")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class UserServiceImpl implements UserService{

    @Resource
    private UserDao userDao;


    /**
     * 查询用户信息
     *
     * @param userid 账号
     * @return 用户信息
     */
    @Transactional(readOnly = true, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public User userInfo(String userid) {
        return userDao.findUserById(Integer.parseInt(userid));
    }

    /**
     * 用户更新
     *
     * @param user 用户实体
     * @return 用户更新标记
     */
    @Transactional(readOnly = false, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean userUpdate(User user) {
        int i = userDao.updateUser(user);
        //更新信息成功
        return i == 1;

    }

    /**
     * 用户注册
     *
     * @param user 用户信息
     * @return 用户注册标记
     */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean userRegister(User user) {
        //存在事务嵌套指定事务的传播行为
        User userInfo = this.userInfo(user.getUserid().toString());
        if (userInfo != null) {
            return false;
        }
        int i = userDao.insertUser(user);
        //注册是否成功
        return i == 1;
    }

    /**
     * 忘记密码
     *
     * @param username 用户名
     * @param password 密码
     * @return 密码修改标记
     */
    @Transactional(readOnly = false, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean userForgetPass(String username, String password) {
        User user = userDao.findUserByEmail(username);
        if (user == null) {
            int userId = 0;
            try {
                userId = Integer.parseInt(username);
            } catch (Exception e) {
                return false;
            }
            user = userDao.findUserById(userId);
            if (user == null) {
                return false;
            } else {
                user.setPassword(password);
                int i = userDao.updateUser(user);
                return i == 1;
            }
        } else {
            user.setPassword(password);
            int i = userDao.updateUser(user);
            return i == 1;
        }
    }


}
